package com.adk.backend.service.impl;

import com.adk.backend.common.PageResult;
import com.adk.backend.entity.LxFeeType;
import com.adk.backend.mapper.FeeTypeMapper;
import com.adk.backend.service.FeeTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 费用类型服务实现类
 */
@Slf4j
@Service
public class FeeTypeServiceImpl extends ServiceImpl<FeeTypeMapper, LxFeeType> implements FeeTypeService {
    
    @Autowired
    private FeeTypeMapper feeTypeMapper;
    
    @Override
    public PageResult<LxFeeType> getFeeTypeList(Map<String, Object> params) {
        Integer pageNum = (Integer) params.getOrDefault("pageNum", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 20);
        
        QueryWrapper<LxFeeType> wrapper = new QueryWrapper<>();
        
        // 按类型筛选
        if (params.get("type") != null) {
            wrapper.eq("type", params.get("type"));
        }
        
        // 按是否成本筛选
        if (params.get("isCost") != null) {
            wrapper.eq("is_cost", params.get("isCost"));
        }
        
        // 按费用名称模糊查询
        if (params.get("feeName") != null && !params.get("feeName").toString().isEmpty()) {
            wrapper.like("fee_name", params.get("feeName"));
        }
        
        wrapper.orderByAsc("id");
        
        Page<LxFeeType> page = new Page<>(pageNum, pageSize);
        Page<LxFeeType> result = feeTypeMapper.selectPage(page, wrapper);
        
        PageResult<LxFeeType> pageResult = new PageResult<>();
        pageResult.setList(result.getRecords());
        pageResult.setTotal(result.getTotal());
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setTotalPages((int) result.getPages());
        
        return pageResult;
    }
    
    @Override
    public LxFeeType getFeeTypeById(Long id) {
        return feeTypeMapper.selectById(id);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public LxFeeType createFeeType(LxFeeType feeType) {
        feeTypeMapper.insert(feeType);
        return feeType;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateFeeType(LxFeeType feeType) {
        return feeTypeMapper.updateById(feeType) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFeeType(Long id) {
        return feeTypeMapper.deleteById(id) > 0;
    }
    
    @Override
    public List<LxFeeType> getAllFeeTypes(Integer type, Integer isCost) {
        QueryWrapper<LxFeeType> wrapper = new QueryWrapper<>();
        
        if (type != null) {
            wrapper.eq("type", type);
        }
        
        if (isCost != null) {
            wrapper.eq("is_cost", isCost);
        }
        
        wrapper.orderByAsc("id");
        
        return feeTypeMapper.selectList(wrapper);
    }
}

